<?php
namespace app\web\controller;

use app\model\Theme; 

class Qrcode extends Base
{
    public function Qrcode()
    {
global $_G, $_P;
$do = 'qrcode';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid = intval($_P['themeid']);
if($themeid <= 0){
    $themeid = Theme::getDefaultThemenew();
}
$system = $_G['system'];
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
    return_msg('还没有创建活动,请联系管理员');
}
$themelist = Theme::getAllTheme();
$config =   Theme::getConfig($themeid);
if ($system['wxapp']!=0 && $system['softmode']!=0) {
    $xcxurl = 'pages/index/index?softname=' . MODULE_NAME . '&themeid=' . $themeid . '&softkey=' . $theme['softkey'];
}
if ($system['is_multiple_merchants']){
    $shoplist = db_select ( "SELECT * FROM " . tablename ( t_shangjia ) ." where state = 1" );
}
//增加对接服务器判断
if (DOMAIN1 == "www.yixuan.net" && $op=='display') {
    //判断当前小程序是否对接
    $account_item = db_get(account,['uniacid'=>$system['wxapp'],'type'=>7]);
    if ($account_item){
        ThirdWxService::init($system['wxapp'],$system['appidxcx']);
        $third_template_list = ThirdWxService::getTemplateList();
        $third_wxapp_item = db_get(third_wxapp,['uniacid'=>$system['wxapp'],'appid'=>$system['appidxcx']]);
        if ($third_wxapp_item && ($third_wxapp_item['status']==2 || $third_wxapp_item['status']==0 || $third_wxapp_item['status']==3)){
            //查询审核状态
            $get_audio_res = ThirdWxService::latestAudioStatus($system['appidxcx']);
            $get_audio_info = $get_audio_res['data'];
            if ($get_audio_info && $get_audio_info['authorization']['release']!=2){
                $data = ['status'=>$get_audio_info['authorization']['release'],'ip'=>$_G['clientip'],'admin_name'=>(string)$_G['username']];
                if ($get_audio_info['res']['user_version']){
                    $data['online_version']=$get_audio_info['res']['user_version'];
                }
                $status_desc = [];
                if ($get_audio_info['res']['reason']){
                    $status_desc['desc']= $get_audio_info['res']['reason'];
                }
                if ($get_audio_info['res']['imgurl']){
                    $status_desc['imgs']= $get_audio_info['res']['imgurl'];
                }
                if ($status_desc){
                    $data['status_desc']=json_encode($status_desc);
                }
                $where = ['uniacid'=>$system['wxapp'],'appid'=>$system['appidxcx']];
                $res = db_update(third_wxapp,$data,$where);
                $third_wxapp_item = db_get(third_wxapp,['uniacid'=>$system['wxapp'],'appid'=>$system['appidxcx']]);
            }
        }
    }
}
//提审
if ($op=='submitAudit'){
    $third_template_id = isset($_P['third_template_id'])?$_P['third_template_id']:'';
    if (!$third_template_id){
        show_json(0,['msg'=>'未选择上传版本']);
    }
    $appid = $system['appidxcx'];
    $uniacid = $system['wxapp'];
    $scene = MODULE_NAME . '-'. $themeid.'-0-'.$theme['softkey'].'-0';
    $template_id = explode(';',$third_template_id)[0];
    $user_version = explode(';',$third_template_id)[1];
    $user_desc = explode(';',$third_template_id)[2];
    $format_ext_json = ThirdWxService::formatScene($appid,$uniacid,$scene,$user_version);
    $res = ThirdWxService::submitAudit($appid,$format_ext_json,$template_id,$user_version,$user_desc);
    if ($res['code']==1){
        //成功
        $op_log = date("Y-m-d H:i:s").$_G['username'].'操作提审小程序;提审版本:'.$user_version;
        $data = ['status'=>2,'ext_json'=>json_encode($format_ext_json),'op_log'=>$op_log,'ip'=>$_G['clientip'],'admin_name'=>(string)$_G['username'],'template_id'=>$template_id];
        $where = ['uniacid'=>$uniacid,'appid'=>$appid];
        $res = db_update(third_wxapp,$data,$where);
        return show_json(1,['msg'=>'操作成功','data'=>[]]);
    }else{
        //失败
        return show_json(0,['msg'=>$res['msg'],'data'=>[]]);
    }
}
//版本回退
if ($op=='revertCodeRelease'){
    $res = ThirdWxService::revertCodeRelease($system['appidxcx']);
    $third_template_list = ThirdWxService::getTemplateList();
    if ($res['code']==1){
        //更新
        $roll_back_version = explode(";",$res['data']['release_info'])[0];
        $op_log = date("Y-m-d H:i:s").$_G['username'].'操作版本回退；回退版本：'.$roll_back_version;
        $data = ['online_version'=>$roll_back_version,'op_log'=>$op_log,'ip'=>$_G['clientip'],'admin_name'=>(string)$_G['username']];
        if ($roll_back_version==$third_template_list['default']['user_version']){
            $data['template_id'] = $third_template_list['default']['template_id'];
        }else{
            foreach ($third_template_list['list'] as $k => $v){
                if ($roll_back_version==$v['user_version']){
                    $data['template_id'] = $v['template_id'];
                    break;
                }
            }
        }
        $scene = MODULE_NAME . '-'. $themeid.'-0-'.$theme['softkey'].'-0';
        $format_ext_json = ThirdWxService::formatScene($system['appidxcx'],$system['wxapp'],$scene,$roll_back_version);
        if ($format_ext_json){
            $data['ext_json'] = json_encode($format_ext_json);
        }
        $where = ['uniacid'=>$system['wxapp'],'appid'=>$system['appidxcx']];
        $res1 = db_update(third_wxapp,$data,$where);
        if ($res1){
            return show_json(1,['msg'=>'操作成功','data'=>[]]);
        }
        return show_json(0,['msg'=>'操作失败','data'=>[]]);
    }else{
        return show_json(0,['msg'=>$res['msg'],'data'=>$res['data']]);
    }
}
//切换对接模式
if ($op=='changeInterfaceMode'){
    $mode = isset($_P['mode'])?$_P['mode']:'hand';
    //切换成手动对接模式
    if ($mode=='hand'){
        $account_item = db_get(account,['uniacid'=>$system['wxapp'],'type'=>7]);
        if (!$account_item){
            show_json(100,['msg'=>'未查询到绑定小程序信息']);
        }
        $where = [];
        $res = db_update(account,['type'=>4],['uniacid'=>$account_item['uniacid'],'acid'=>$account_item['acid']]);
        if (!$res){
            show_json(100,['msg'=>'切换对接模式失败']);
        }
    }else{
        //判断是否存在对接记录
    }
    show_json(200,['msg'=>'切换对接模式成功']);
}
$return_clicent = get_defined_vars();
return view('/qrcode' , $return_clicent);
}
}